Conditions | 3 |
Paths | > 20000 |
Total Lines | 400 |
Lines | 24 |
Ratio | 6 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | ;(function($, window, document, undefined) { |
||
445 | $(document).ready(function() { |
||
446 | editMode = window.editMode || false; |
||
447 | lang = window.lang || {}; |
||
448 | phpbb = window.phpbb || {}; |
||
449 | tinymce = window.tinymce || {}; |
||
450 | twig = window.twig || {}; |
||
451 | config = window.config || { |
||
452 | ajaxUrl: '', |
||
453 | boardUrl: '', |
||
454 | route: '', |
||
455 | ext: '', |
||
456 | style: '' |
||
457 | }; |
||
458 | |||
459 | var copyFrom = ''; |
||
460 | var body = {}; |
||
461 | var blocksPanel = {}; |
||
462 | var exPositions = {}; |
||
463 | var overPosition = {}; |
||
464 | var isHidingBlocks = false; |
||
465 | |||
466 | var loader = $('#admin-bar').show().find('#admin-control').click(function() { |
||
467 | if (editMode) { |
||
468 | $(this).toggleClass('admin-bar-toggler').prev().toggle(); |
||
469 | body.toggleClass('push-down'); |
||
470 | return false; |
||
471 | } |
||
472 | }).find('i'); |
||
473 | |||
474 | if (editMode) { |
||
475 | inlineForm = $('<form class="inline-form"><input type="text" class="inline-edit" value="" /></form>').hide().appendTo($('body')); |
||
476 | |||
477 | msgObj = $('#ajax-message'); |
||
478 | emptyPositionsObj = $('.block-position:not(:has(".block"))').addClass('empty-position'); |
||
479 | |||
480 | template = twig({ |
||
481 | data: $.trim($('#block-template-container').html()) |
||
482 | }); |
||
483 | |||
484 | $('#add-block-panel').find('.sitemaker-block').draggable({ |
||
485 | addClasses: false, |
||
486 | iframeFix: true, |
||
487 | opacity: 0.7, |
||
488 | helper: 'clone', |
||
489 | appendTo: 'body', |
||
490 | revert: 'invalid', |
||
491 | connectToSortable: '.block-position', |
||
492 | start: function(event, ui) { |
||
493 | $(ui.helper).addClass('dragging'); |
||
494 | showAllPositions(); |
||
495 | blockPositions.sortable('refresh'); |
||
496 | blocksPanel.trigger('click'); |
||
497 | }, |
||
498 | stop: function() { |
||
499 | window.setTimeout(function() { |
||
500 | if (updated === false) { |
||
501 | hideEmptyPositions(); |
||
502 | } |
||
503 | }, 600); |
||
504 | } |
||
505 | }); |
||
506 | |||
507 | exPositions = $('#ex_positions'); |
||
508 | blockPositions = $('.block-position').addClass('block-receiver').sortable({ |
||
509 | revert: true, |
||
510 | placeholder: 'ui-state-highlight grid__col sm-block-spacing block sortable placeholder', |
||
511 | connectWith: '.block-position', |
||
512 | cancel: '.editable-block, .inline-edit', |
||
513 | items: '.block', |
||
514 | tolerance: 'pointer', |
||
515 | cursor: 'move', |
||
516 | cursorAt: { |
||
517 | top: -10, |
||
518 | left: -10 |
||
519 | }, |
||
520 | start: function(event, ui) { |
||
521 | origin = $(ui.item).addClass('dragging').parent('.horizontal'); |
||
522 | showAllPositions(); |
||
523 | blockPositions.sortable('refresh'); |
||
524 | }, |
||
525 | over: function(event, ui) { |
||
526 | overPosition = ui.placeholder.parent('.horizontal').children('.block').addClass('sortable'); |
||
527 | }, |
||
528 | out: function() { |
||
529 | if ($.isEmptyObject(overPosition) === false) { |
||
530 | overPosition.removeClass('sortable'); |
||
531 | } |
||
532 | }, |
||
533 | update: function(event, ui) { |
||
534 | updated = true; |
||
535 | if ($(ui.item).attr('id') === undefined) { |
||
536 | var posID = $(this).attr('id').substring(4); |
||
537 | var blockName = $(ui.item).attr('data-block'); |
||
538 | var replace = $(this).find('div[data-block="' + blockName + '"]'); |
||
539 | addBlock(posID, blockName, replace); |
||
540 | } else { |
||
541 | saveBtn.button('enable'); |
||
542 | } |
||
543 | |||
544 | var items = $(ui.item).removeAttr('style').parent('.horizontal').children('.block'); |
||
545 | var item = $(ui.item); |
||
546 | |||
547 | item.parent().removeClass('empty-position'); |
||
548 | removeGrid(item); |
||
549 | |||
550 | if (items.length > 0) { |
||
551 | sortHorizontal(items); |
||
552 | } |
||
553 | |||
554 | if (origin.attr('id') !== $(ui.item).parent('.horizontal').attr('id')) { |
||
555 | sortHorizontal(origin.find('.block').removeClass('sortable')); |
||
556 | } |
||
557 | }, |
||
558 | stop: function(event, ui) { |
||
559 | $(ui.item).removeClass('dragging sortable').removeAttr('style'); |
||
560 | hideEmptyPositions(); |
||
561 | $('body').trigger('layoutChanged'); |
||
562 | } |
||
563 | }).on('click', '.block-title', function() { |
||
564 | makeEditable($(this)); |
||
565 | }).on('submit', '.inline-form', function(e) { |
||
566 | e.preventDefault(); |
||
567 | $(this).find('.inline-edit').trigger('blur'); |
||
568 | }).on('focusout', '.inline-edit', function() { |
||
569 | processInput($(this)); |
||
570 | }).on('click', '.edit-block', function(e) { |
||
571 | e.preventDefault(); |
||
572 | blockObj = $(this).parentsUntil('.block').parent(); |
||
573 | getEditForm(blockObj); |
||
574 | }).on('click', '.delete-block', function(e) { |
||
575 | e.preventDefault(); |
||
576 | blockObj = $(this).parentsUntil('.block').parent(); |
||
577 | dialogConfirm.dialog({buttons: dButtons}).dialog('open'); |
||
578 | }).each(function() { |
||
579 | var pos = $(this).attr('id').substring(4); |
||
580 | if (exPositions.find('option[value=' + pos + ']').length === 0) { |
||
581 | exPositions.append('<option value="' + pos + '">' + pos + '</option>'); |
||
582 | } |
||
583 | }); |
||
584 | |||
585 | saveBtn = $('#toggle-edit').button().click(function() { |
||
586 | // exit edit mode |
||
587 | }).parent().next().children('a').button({disabled: true}).click(function(e) { |
||
588 | // save changes |
||
589 | e.preventDefault(); |
||
590 | saveLayout(); |
||
591 | }); |
||
592 | |||
593 | $('.has-dropdown').click(function(e) { |
||
594 | e.preventDefault(); |
||
595 | $(this).parentsUntil('ul').parent().find('.dropped').not($(this)).removeClass('dropped').next().hide(); |
||
596 | blocksPanel = $(this).toggleClass('dropped'); |
||
597 | blocksPanel.next().toggle(); |
||
598 | }).next().mouseleave(function() { |
||
599 | //$(this).prev().trigger('click'); |
||
600 | }); |
||
601 | |||
602 | $('#admin-options').show(100, function() { |
||
603 | var exPos = $.grep(exPositions.val(), function(str) { |
||
604 | return str.length; |
||
605 | }); |
||
606 | showCurrentState(isHidingBlocks, exPos); |
||
607 | |||
608 | // Thanks KungFuJosh, for this tip |
||
609 | body = $('body').addClass('push-down'); |
||
610 | }); |
||
611 | |||
612 | // Only show style selector if there are other styles |
||
613 | var styleSelector = $('#style-options'); |
||
614 | if (styleSelector.find('option').length > 1) { |
||
615 | styleSelector.show(); |
||
616 | } |
||
617 | |||
618 | $.ajaxSetup({ |
||
619 | // add style id to ajax requests |
||
620 | 'beforeSend': function(xhr, settings) { |
||
621 | loader.addClass('fa-spinner fa-green fa-spin fa-lg fa-pulse'); |
||
622 | settings.url += ((settings.url.indexOf('?') < 0) ? '?' : '&') + 'style=' + config.style; |
||
623 | }, |
||
624 | 'complete': function(data) { |
||
625 | loader.delay(2000).removeClass('fa-spinner fa-green fa-spin fa-lg fa-pulse'); |
||
626 | |||
627 | if (data.responseJSON) { |
||
628 | // Display any returned message |
||
629 | if (data.responseJSON.message) { |
||
630 | showMessage(data.responseJSON.message); |
||
631 | } |
||
632 | } |
||
633 | }, |
||
634 | 'error': function(event) { |
||
635 | showMessage((event.responseJSON && event.responseJSON.message) ? event.responseJSON.message : lang.ajaxError); |
||
636 | } |
||
637 | }); |
||
638 | |||
639 | eButtons[lang.edit] = function() { |
||
640 | saveForm(blockObj); |
||
641 | }; |
||
642 | |||
643 | eButtons[lang.cancel] = function() { |
||
644 | undoPreviewBlock(); |
||
645 | $(this).dialog('close'); |
||
646 | }; |
||
647 | |||
648 | dButtons[lang.remove] = function() { |
||
649 | var horizontalPos = blockObj.parent('.horizontal'); |
||
650 | |||
651 | blockObj.remove(); |
||
652 | horizontalPos.find('.ui-effects-placeholder').remove(); |
||
653 | |||
654 | var items = horizontalPos.find('.block'); |
||
655 | |||
656 | if (items.length > 0) { |
||
657 | sortHorizontal(items); |
||
658 | } |
||
659 | |||
660 | hideEmptyPositions(); |
||
661 | saveBtn.button('enable'); |
||
662 | $(this).dialog('close'); |
||
663 | }; |
||
664 | |||
665 | dButtons[lang.cancel] = function() { |
||
666 | $(this).dialog('close'); |
||
667 | }; |
||
668 | |||
669 | var defDialog = { |
||
670 | autoOpen: false, |
||
671 | modal: true, |
||
672 | width: 'auto', |
||
673 | show: 'slide', |
||
674 | hide: 'slide' |
||
675 | }; |
||
676 | |||
677 | dialogConfirm = $('#dialog-confirm').dialog(defDialog); |
||
678 | |||
679 | // Delete all blocks |
||
680 | lButtons[lang.deleteAll] = function() { |
||
681 | $(this).dialog('close'); |
||
682 | $('.block-position').empty(); |
||
683 | hideEmptyPositions(); |
||
684 | saveBtn.button('enable'); |
||
685 | updated = true; |
||
686 | }; |
||
687 | |||
688 | lButtons[lang.cancel] = function() { |
||
689 | $(this).dialog('close'); |
||
690 | }; |
||
691 | |||
692 | var dialogDeleteAll = $('#dialog-delete-all').dialog(defDialog); |
||
693 | var deleteAll = $('#delete-blocks').button().click(function(e) { |
||
694 | e.preventDefault(); |
||
695 | dialogDeleteAll.dialog({buttons: lButtons}).dialog('open'); |
||
696 | }); |
||
697 | |||
698 | // Initiate dialog for block copy |
||
699 | cButtons[lang.copy] = function() { |
||
700 | $(this).dialog('close'); |
||
701 | copyBlocks(copyFrom); |
||
702 | deleteAll.parent().show(); |
||
703 | }; |
||
704 | |||
705 | cButtons[lang.cancel] = function() { |
||
706 | $(this).dialog('close'); |
||
707 | }; |
||
708 | |||
709 | // Events for add block dropdown |
||
710 | dialogCopy = $('#dialog-copy').dialog(defDialog); |
||
711 | View Code Duplication | $('#copy-form').find('.layout-action').button().click(function(e) { |
|
712 | e.preventDefault(); |
||
713 | copyFrom = $(this).parent().parent().serializeArray(); |
||
714 | |||
715 | var fromRoute = copyFrom[0].value; |
||
716 | var fromStyle = copyFrom[1].value; |
||
717 | var layoutAction = $(this).data('action'); |
||
718 | |||
719 | if (fromRoute === '' || (fromRoute === config.route && fromStyle === config.style)) { |
||
720 | return false; |
||
721 | } |
||
722 | |||
723 | if (layoutAction === 'copy') { |
||
724 | blocksPanel.trigger('click'); |
||
725 | dialogCopy.dialog({buttons: cButtons}).dialog('open'); |
||
726 | } else { |
||
727 | var url = ''; |
||
728 | |||
729 | url += ((fromRoute.substring(0, 1) === '/') ? config.ajaxUrl : config.boardUrl + '/') + fromRoute; |
||
730 | url += ((url.indexOf('?') >= 0) ? '&' : '?') + 'style=' + fromStyle + '&edit_mode=1'; |
||
731 | |||
732 | window.location.href = url; |
||
733 | } |
||
734 | }); |
||
735 | |||
736 | // Events for edit block dialog |
||
737 | defDialog.open = function() { |
||
738 | if (dialogEditOpened === false) { |
||
739 | var pane = $(this).dialog('widget').find('.ui-dialog-buttonpane'); |
||
740 | dialogEditOpened = true; |
||
741 | $('<label class="dialog-check-button"><input id="update-similar" type="checkbox" />' + lang.updateSimilar + '</label>').prependTo(pane); |
||
742 | } |
||
743 | }; |
||
744 | |||
745 | dialogEdit = $('#dialog-edit').dialog(defDialog).on('click', '.block-class-actions', function(e) { |
||
746 | e.preventDefault(); |
||
747 | var action = $(this).data('action'); |
||
748 | var editor = dialogEdit.find('#block_class'); |
||
749 | switch (action) { |
||
750 | case 'clear': |
||
751 | editor.text('').change(); |
||
752 | break; |
||
753 | case 'toggle': |
||
754 | dialogEdit.find('#css-class-options').slideToggle(); |
||
755 | break; |
||
756 | case 'undo': |
||
757 | case 'redo': |
||
758 | editor.focus(); |
||
759 | document.execCommand(action, false, null); |
||
760 | editor.change(); |
||
761 | break; |
||
762 | } |
||
763 | }).on('click', '.class-cat', function(e) { |
||
764 | var id = $(this).attr('href'); |
||
765 | var obj = $('#classes-scroller'); |
||
766 | obj.animate({ |
||
767 | scrollTop: obj.scrollTop() + $(id).position().top |
||
768 | }, 1000); |
||
769 | e.preventDefault(); |
||
770 | }).on('click', '.transform', function(e) { |
||
771 | var editor = dialogEdit.find('#block_class'); |
||
772 | editor.focus(); |
||
773 | document.execCommand('insertText', false, $(this).text() + ' '); |
||
774 | editor.change(); |
||
775 | e.preventDefault(); |
||
776 | }).on('change', '.block-preview', function() { |
||
777 | previewBlock(); |
||
778 | }); |
||
779 | |||
780 | // Set default layout |
||
781 | $('.default-layout').button().click(function(e) { |
||
782 | var setDefault = $(this).data('set'); |
||
783 | setDefaultLayout(setDefault); |
||
784 | e.preventDefault(); |
||
785 | if (setDefault === true) { |
||
786 | // set as default |
||
787 | $(this).parent().hide().next().hide().next().show(); |
||
788 | deleteAll.parent().hide(); |
||
789 | } else { |
||
790 | // remove as default |
||
791 | $(this).parent().hide().prev().hide().prev().show(); |
||
792 | deleteAll.parent().show(); |
||
793 | } |
||
794 | }); |
||
795 | |||
796 | // Set start page |
||
797 | $('.sm-startpage').button().click(function(e) { |
||
798 | e.preventDefault(); |
||
799 | var info = {}; |
||
800 | if ($(this).attr('id') === 'set-startpage') { |
||
801 | // set as startpage |
||
802 | $(this).parent().hide().next().show(); |
||
803 | info = { |
||
804 | controller: $(this).data('controller'), |
||
805 | method: $(this).data('method'), |
||
806 | params: $(this).data('params') |
||
807 | }; |
||
808 | setStartPage(info); |
||
809 | } else { |
||
810 | // remove as startpage |
||
811 | $(this).parent().hide().prev().show(); |
||
812 | setStartPage(info); |
||
813 | } |
||
814 | }); |
||
815 | |||
816 | isHidingBlocks = $('#route-settings').submit(function(e) { |
||
817 | setRoutePrefs($(this)); |
||
818 | e.preventDefault(); |
||
819 | }).find('#hide_blocks').is(':checked'); |
||
820 | |||
821 | window.onbeforeunload = function(e) { |
||
822 | if (updated === true) { |
||
823 | e = e || window.event; |
||
824 | // For IE and Firefox |
||
825 | if (e) { |
||
826 | e.returnValue = lang.leaveConfirm; |
||
827 | } |
||
828 | // For Safari |
||
829 | return lang.leaveConfirm; |
||
830 | } |
||
831 | }; |
||
832 | |||
833 | // Init Icon Picker |
||
834 | $('.sitemaker').iconPicker({ |
||
835 | selector: '.block-icon', |
||
836 | onSelect: function(item, iconClass) { |
||
837 | var id = item.parentsUntil('.block').parent().attr('id').substring(6); |
||
838 | updateBlock({'id': id, 'icon': iconClass}); |
||
839 | } |
||
840 | }); |
||
841 | |||
842 | initTinyMce(); |
||
843 | } |
||
844 | }); |
||
845 | })(jQuery, window, document); |